home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / VideoToolbox / VideoToolboxSources / MoveMouse.c < prev    next >
Encoding:
Text File  |  1995-10-06  |  8.0 KB  |  197 lines  |  [TEXT/CWIE]

  1. /*
  2. MoveMouse.c
  3.  
  4. WARNING:
  5.     (Copied from Apple's CursorDevices.h in Universal Headers 2.1, ETO18. dgp 10/95)
  6.     On currently shipping PowerMacs, the CursorDevices manager is implemented
  7.     in 68K code and emulated.  Unfortunately, the MixedMode glue in InterfaceLib
  8.     is incorrect.  It and the 1.0 version of this file had incorrect parameter
  9.     lists for most functions.
  10.     
  11.     As a first step to avoid runtime errors, the functions in this file were 
  12.     renamed (e.g. from"CrsrDevButtons" to "CursorDeviceButtons").  This will result
  13.     in a link time error if a PowerPC application tries to call the functions.
  14.     When InterfaceLib is fixed, the new names will be exported and PowerPC
  15.     code will then be able to call them.
  16.  
  17. The code in this file is from Apple. It may be useful to people who need an equivalent to
  18. SetMouse.c that works on ALL Macs. - dgp, 12/94
  19.  
  20. Dan Sears (sears@netcom.com) writes, 
  21. "The Apple file MoveMouse.c is based on some code from Jon Wtte and it uses the
  22. Cursor Device Manager.  You should read the tech note that describes the
  23. API for this manager, "HW 01 - ADB (The Untold Story: Space Aliens ate my
  24. mouse)". 
  25.  
  26. NOTE: The THINK C 6 compiler choked on one of the (legal) recursive structure
  27. definitions in the original version of this file. (The structure definitions
  28. are now all in Apple's CursorDevices.h file.) The THINK C 7 and CodeWarrior
  29. compilers don't complain.
  30.  
  31. From Apple Developer Services:
  32.  
  33. In the past, moving the cursor programatically on the Macintosh has
  34. entailed the manipulation of low memory globals. This has worked for
  35. all macs for quite some time. But with the advent of the power book
  36. series and many 3rd party track balls, it has become apparent that the
  37. low memory globals no longer contained adequate information for the
  38. system to truly support all types of positioning devices. So, with the
  39. new Centris CPU's, and CPU's from all lines after we are implementing a
  40. new tool set that provides a much more comprehensive set of utilities
  41. for dealing with the cursor location and mouse button state. We call
  42. this new manager the Cursor Device Manager, and DTS will be documenting
  43. it in a future tech not, but since many machines are now shipping that
  44. do not use the traditional low memory globals to get the mouse location
  45. and position the mouse, it is important to "leak" a little of the tech
  46. note information early.
  47.  
  48. First, the CDM (Cursor device manager) has its own trap ($AADB) and you
  49. can determine if a machine supports the CDM by using the standard TrapAvailable
  50. routine. (See inside mac for the source code to Trap
  51. Available). Once you have determined that the CDM exists, you should
  52. try not to use the low memory globals RawMouse and MTemp to get the
  53. current cursor location, or set a new cursor location. Instead the CDM
  54. provides 3 calls which you can use to get this info, the first is
  55. CrsrDevNextDevice which will give you the next cursor device record in
  56. the CDM list, if you pass it a NULL, it will give you the head of the
  57. list. Once you have the first cursor device you can use it to get the
  58. Cursor data record which contains the current cursor location in its
  59. where field. You can set the current location with either the
  60. CrsrDevMove (which moves relative to the current location) or the
  61. CrsrDevMoveTo (which moves to an absolute location) routines.
  62.  
  63. The following is some sample code which implement some generic routines
  64. that work on all macintoshes. These routines are written not for speed
  65. but for clarity, so if you are writing a device driver or time critical
  66. code there are some things that you can do to speed up these routines,
  67. like predetermine at the outset if the CDM exists, this way you
  68. wouldn't have to check every time you want to use it, and getting the
  69. cursor device only once and keeping the pointer to it around between
  70. calls.
  71.  
  72. Anyway, here is some sample code that moves the mouse diagonally until
  73. the mouse button is pressed. For a further explanation of how the older
  74. low memory global method works, see the developer support tech info
  75. database on AppleLink.
  76.  
  77. HISTORY:
  78. 8/94 dgp Downloaded from ftp://nada.kth.se/pub/hacks/mac-faq/MoveMouse.c
  79. 10/94 dgp made minimal changes so that it would compile in CodeWarrior C. Untested.
  80. 12/94 dgp updated for compatibility with Universal Header "CursorDevices.h". Untested.
  81. 1/5/95 dgp updated for compatiblity with Universal Headers 2, in which CursorDevices.h was
  82. extensively changed. Untested.
  83. 6/26/95 dgp cosmetic changes. Still untested.
  84. */
  85.  
  86. #include "VideoToolbox.h"    // TrapAvailable
  87. #if !UNIVERSAL_HEADERS
  88.     #error "Sorry, this file cannot be compiled without Apple's Universal Header files, preferably v. 2"
  89. #endif
  90. #include <CursorDevices.h>
  91. #define _CursorDeviceDispatch 0xAADB // new cursor device manager trap 
  92. #if GENERATINGPOWERPC
  93.     #define CallCursorTask()    // not needed on powerpc, so give empty definition
  94. #else
  95.     pascal void CallCursorTask(void) = {0x2078,0x08EE,0x4E90};
  96.     /*
  97.         MOVE.L jCrsrTask,A0
  98.         JSR (A0)
  99.     */
  100. #endif
  101. // Old-style mouse-moving equates for the low memory globals
  102. #define xRawMouse 0x082C    // low memory global that has current mouse loc
  103. #define xMTemp 0x0828 // low memory global that has current mouse loc
  104. #define xCrsrNew 0x08CE // set after you change mtemp and rawmouse
  105. #define xCrsrCouple 0x08CF // true if the cursor is tied to the mouse
  106.  
  107. void GetMouseDevicePosition(Point *currentPoint);
  108. void SetMouseDevicePosition(Point newPoint);
  109. void SetMouseDevicePositionRel(Point newPoint);
  110. void FudgeCursor(void);
  111.  
  112. #if UNIVERSAL_HEADERS<2
  113.     #define CursorDevice CrsrDevice
  114.     #define CursorDeviceNextDevice CrsrDevNextDevice
  115.     #define CursorDeviceMove CrsrDevMove
  116.     #define CursorDeviceMoveTo CrsrDevMoveTo
  117. #endif
  118.  
  119. void GetMouseDevicePosition(Point *currentPoint)
  120. {
  121.     // This routine returns the current cursor location
  122.     CursorDevice *firstMouse;
  123.     if (TrapAvailable(_CursorDeviceDispatch)) { 
  124.         // If we get here we have the CDM
  125.         firstMouse = NULL;    // start at head of cursor dev list
  126.         CursorDeviceNextDevice(&firstMouse);    // get the next cursor device
  127.         // Now get the current cursor location from the Cursor data
  128.         *currentPoint = firstMouse->whichCursor->where;
  129.     } else {
  130.         // No CDM so we use the low memory global
  131.         *currentPoint = *(Point *)xRawMouse;
  132.     }
  133. }
  134.  
  135. void SetMouseDevicePosition(Point newPoint)
  136. {
  137.     // This routine sets the mouse position to the passed point
  138.     CursorDevice *firstMouse;
  139.  
  140.     if (TrapAvailable(_CursorDeviceDispatch)) { 
  141.         // If we get here we have the CDM
  142.         firstMouse = NULL;    // start at head of cursor dev list
  143.         CursorDeviceNextDevice(&firstMouse); // get the next cursor device
  144.         // Call CDM to move the mouse
  145.         CursorDeviceMoveTo(firstMouse,(long)newPoint.h,(long)newPoint.v);
  146.     } else {
  147.         // No CDM so we use the low memory globals
  148.         *(Point *)xRawMouse = newPoint;
  149.         *(Point *)xMTemp = newPoint;
  150.         *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple; // Set CrsrNew if coupled
  151.         CallCursorTask();    // must call jCrsrTask to update system
  152.     }
  153. }
  154.  
  155. void SetMouseDevicePositionRel(Point newPoint)
  156. {
  157.     // Adds the passed point to the current mouse location
  158.     CursorDevice *firstMouse;
  159.     Point tempPt;
  160.     if (TrapAvailable(_CursorDeviceDispatch)) { 
  161.         // If we get here we have the CDM
  162.         firstMouse = NULL;    // start at head of cursor dev list
  163.         CursorDeviceNextDevice(&firstMouse);    // get the next cursor device
  164.         // Call CDM to move the mouse relative
  165.         CursorDeviceMove(firstMouse,(long)newPoint.h,(long)newPoint.v);
  166.     } else {
  167.         // No CDM so we use the low memory globals
  168.         tempPt = *(Point *)xRawMouse;
  169.         tempPt.h += newPoint.h;
  170.         tempPt.v += newPoint.v;
  171.         *(Point *)xRawMouse = tempPt;
  172.         *(Point *)xMTemp = tempPt;
  173.         *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple;
  174.         CallCursorTask();
  175.     }
  176.     
  177. void FudgeCursor(void)
  178. {
  179. // Now here is a routine that you can drop into the traffic light sample that
  180. // can be called to slowly move the mouse to the lower right hand corner of 
  181. // the screen it exits when the mouse button is held down
  182.  
  183.     Point RandPt;
  184.     long fred;
  185.  
  186.     GetMouseDevicePosition(&RandPt);
  187.     do {
  188.     RandPt.h += 1; // Bump to the next pixel across
  189.     RandPt.v += 1; // Bump to the next pixel down
  190.     
  191.     SetMouseDevicePosition(RandPt); // move absolute
  192.     
  193.     Delay(10,&fred); // wait 10 ticks for smooth drawing
  194.     } while (!Button()); // quit when the button is down.
  195. }
  196.